﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleApp10;

namespace WinFormsApp1
{
    public partial class Form2 : Form
    {
        private Order showedOrder;
        private int rowIndex = 0;
        private Form1 form1;
        private int signalofOpe;
        private Size formInitialSize;
        private Dictionary<Control, Rectangle> controlInitialBounds = new Dictionary<Control, Rectangle>();

        public Form2(Order Ord, string user,Form1 MainForm,int s)
        {
            InitializeComponent();
            this.Load += Form1_Load;
            this.Resize += Form1_Resize;
            showedOrder = Ord;
            form1 = MainForm;
            signalofOpe = s;
            this.NameofCustomer.Text = user;
            if (Ord.Id != -1)
            {
                this.IdofOrder.Text = Ord.Id.ToString();
                FillOrderDetail(Ord, this.dataGridView1);
            }
        }
        //记录初始大小
        private void Form1_Load(object sender, EventArgs e)
        {
            // 记录窗体的初始大小
            formInitialSize = this.Size;

            // 遍历窗体中的所有控件，记录它们的初始大小和位置
            foreach (Control control in this.Controls)
            {
                controlInitialBounds[control] = control.Bounds;
            }
        }
        //控件跟随缩放
        private void Form1_Resize(object sender, EventArgs e)
        {
            // 计算窗体的缩放比例
            float scaleX = (float)this.Width / formInitialSize.Width;
            float scaleY = (float)this.Height / formInitialSize.Height;

            // 遍历所有控件，根据缩放比例调整大小和位置
            foreach (Control control in controlInitialBounds.Keys)
            {
                Rectangle initialBounds = controlInitialBounds[control];
                control.Bounds = new Rectangle(
                    (int)(initialBounds.X * scaleX),
                    (int)(initialBounds.Y * scaleY),
                    (int)(initialBounds.Width * scaleX),
                    (int)(initialBounds.Height * scaleY)
                );
            }
        }
        //显示表格内容
        private void FillOrderDetail(Order o, DataGridView d)
        {
            d.Rows.Clear();
            for (int i = 0; i < o.orderDetails.goods.Count; i++)
            {
                d.Rows.Add(o.orderDetails.goods[i].Name, o.orderDetails.goods[i].Price, o.orderDetails.num[i]);
            }
        }
        //订单中添加商品
        private void buttonofAddGoods_Click(object sender, EventArgs e)
        {
            try
            {
                errormass.Text = "";
                double price = double.Parse(textBox4.Text);
                int num = int.Parse(textBox5.Text);
                Goods newgoods = new Goods(textBox3.Text, price);
                showedOrder.orderDetails.Add(newgoods, num);
                FillOrderDetail(showedOrder, dataGridView1);
            }
            catch
            {
                errormass.Text = "Add Failed!";
            }
        }
        //删除商品
        private void buttonofDelGoods_Click(object sender, EventArgs e)
        {
            errormass.Text = "";
            dataGridView1.Rows.RemoveAt(rowIndex);
            showedOrder.orderDetails.goods.Remove(showedOrder.orderDetails.goods[rowIndex]);
            showedOrder.orderDetails.num.Remove(showedOrder.orderDetails.num[rowIndex]);
        }
        //获取用户在表格中选择的项
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            rowIndex = e.RowIndex;
        }
        //返回主窗体
        private void buttonofEndEdit_Click(object sender, EventArgs e)
        {
            errormass.Text = "";
            showedOrder.Id = int.Parse(IdofOrder.Text);
            showedOrder.Customer = NameofCustomer.Text;
            if (signalofOpe == 1)
                form1.AddNewOrderService(showedOrder);
            else if (signalofOpe == 2)
                form1.EditNewOrderService(showedOrder);
        }
    }
}
